package com.example.finaltask.Adapter;

import static android.view.View.*;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.finaltask.LoginActivity;
import com.example.finaltask.R;
import com.example.finaltask.modle.Poster;
import com.example.finaltask.modle.User;
import com.example.finaltask.utils.DBHelper;
import com.example.finaltask.utils.DateFormat;
import com.example.finaltask.utils.RoundImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ListAdapter extends BaseAdapter {
    private List<?> list;
    private SQLiteDatabase db;
    private LayoutInflater layoutInflater;//布局服务
    private Context mContext;
    private User user;
    private Poster p;

    //构造函数Adapter，在创建这个适配器对象的时候，将所有数据都传入，以便进行之后的操作。
    public ListAdapter(Context context, List<?> list,User user) {
        this.list = list;
        this.mContext = context;
        this.user = user;
        this.layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 组件集合，对应list.xml中的控件
     *
     * @author Administrator
     */
    static class Component {
        RoundImageView headImage;//圆角图片 头像
        TextView title;//标题
        TextView content;//内容
        TextView time;//时间
        TextView poster;
        RoundImageView image;//图片
        Button delete;//按钮
        LinearLayout menu;//线性布局 菜单
    }

    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    @Override

    public View getView(final int position, View convertView, ViewGroup parent) {
        //当前操作的用户
        //user = (User) data.get(position).get("currentUser");
        //assert user!=null;
        final Component component;
        //convertView是否为空null，如果非空，则直接再次对convertView复用，否则才创建新的View
        if (convertView == null) {
            component = new Component();
            //获得组件,实例化组件
            convertView = layoutInflater.inflate(R.layout.item_dynamic, null);
            component.headImage = convertView.findViewById(R.id.item_activity_head_image);
            component.poster = convertView.findViewById(R.id.item_activity_poster_name);
            component.time = convertView.findViewById(R.id.item_activity_poster_time);
            component.title = convertView.findViewById(R.id.item_activity_poster_title);
            component.content=convertView.findViewById(R.id.item_activity_poster_content);
            component.image = convertView.findViewById(R.id.item_activity_poster_image);
            component.delete = convertView.findViewById(R.id.delete);
            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }
        //绑定数据
        final Poster currentPoster = (Poster) list.get(position);
        String preImage = currentPoster.getPreviewImageUrl();
        //如果没有头像,用默认头像
        if("".equals(preImage)){
            Glide.with(mContext).load("https://tse1-mm.cn.bing.net/th/id/OIP-C.6QZc9ezh-imkA3vvqXzThQHaHa?pid=ImgDet&rs=1").into(component.image);
        }else {
            Glide.with(mContext).load(preImage).into(component.image);
        }
        component.title.setText(currentPoster.getTitle());
        component.content.setText(currentPoster.getContent());
        component.poster.setText(user.getUserName());
        component.time.setText("发布于" + currentPoster.getCreateTime());
        Glide.with(mContext).load(user.getHeadImage()).into(component.headImage);
        //点击删除
        component.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //assert p != null;
                confirmDialog("确认删除动态？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //LitePal.delete(Dynamic.class,dynamic.getId());
                        db.rawQuery("DELETE FROM poster WHERE id = ?", new String[]{String.valueOf(p.getId())});
                    }
                });
                component.menu.setVisibility(View.GONE);
            }
        });
        return convertView;
    }
        /**
         * * @description 选择确认框
         */
        private void confirmDialog (String message, DialogInterface.OnClickListener listener){
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请确认");
            builder.setMessage(message);
            builder.setPositiveButton("是😞", listener);
            builder.setNegativeButton("否", null);
            builder.show();
        }
        /**
         * @description 提示框
         */
        private void showDialog (String title, String message){
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle(title);
            builder.setMessage(message);
            builder.setPositiveButton("是", null);
            builder.show();
        }
    }